package com.example.administrator.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        private static final String[] PROJECTION = new String[] {
                Notes._ID, // 0
                Notes.TITLE, // 1
        };
        Intent intent = getIntent();
        if (intent.getData() == null)
        {
            intent.setData(Notes.CONTENT_URI);
        }
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.noteslist_item, cursor,
                new String[] { Notes.TITLE }, new int[] { android.R.id.text1 });
        setListAdapter(adapter);
        @Override
        public String getType(Uri uri) {
            switch (sUriMatcher.match(uri)) {
                case NOTES:
                    return Notes.CONTENT_TYPE;
                case NOTE_ID:
                    return Notes.CONTENT_ITEM_TYPE;
                default:
                    throw new IllegalArgumentException("Unknown URI " + uri);
            }
        }
        package cn.dccssq;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

        public class MainActivity extends ListActivity {

            private static final int INSERT_ID = Menu.FIRST;

            private static final int DELETE_ID = Menu.FIRST + 1;

            private static final int ACTIVITY_CREATE = 0;

            private static final int ACTIVITY_EDIT = 1;

            private DiaryDbAdapter diaryDb;

            private Cursor cursor;
            /** Called when the activity is first created. */
            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);

                diaryDb =new DiaryDbAdapter(this);
                diaryDb.open();

            }

            private void showListView(){
                cursor = diaryDb.getAllNotes();

                String[] from = new String[]{DiaryDbAdapter.KEY_TITLE,DiaryDbAdapter.KEY_BODY};
                int[] to = new int[]{R.id.text1,R.id.created};

                ListAdapter cursorAdapter = new SimpleCursorAdapter(this,R.layout.diary_row,cursor,from,to);
                setListAdapter(cursorAdapter);
            }

            @Override
            protected void onListItemClick(ListView l, View v, int position, long id) {
                // TODO Auto-generated method stub
                super.onListItemClick(l, v, position, id);
                Cursor c = cursor;
                c.move(position);
                Intent intent = new Intent(this,ActivityDiary.class);
                intent.putExtra(DiaryDbAdapter.KEY_ROWID, id);
                intent.putExtra(DiaryDbAdapter.KEY_TITLE, c.getString(c
                        .getColumnIndexOrThrow(DiaryDbAdapter.KEY_TITLE)));
                intent.putExtra(DiaryDbAdapter.KEY_BODY, c.getString(c
                        .getColumnIndexOrThrow(DiaryDbAdapter.KEY_BODY)));
                startActivityForResult(intent, ACTIVITY_EDIT);
            }

            @Override
            protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                // TODO Auto-generated method stub
                super.onActivityResult(requestCode, resultCode, data);
                showListView();
            }

            @Override
            public boolean onCreateOptionsMenu(Menu menu) {
                // TODO Auto-generated method stub
                super.onCreateOptionsMenu(menu);
                menu.add(0,INSERT_ID,0,R.string.menu_insert);
                menu.add(0,DELETE_ID,0,R.string.menu_delete);
                return true;
            }

            @Override
            public boolean onMenuItemSelected(int featureId, MenuItem item) {
                // TODO Auto-generated method stub
                switch(item.getItemId()){
                    case INSERT_ID:
                        Log.i("INSERT:", String.valueOf(INSERT_ID));
                        createDiary();
                        return true;
                    case DELETE_ID:
                        Log.i("DELETE_ID:", String.valueOf(getListView().getSelectedItemId()));
                        diaryDb.deleteDiary(getListView().getSelectedItemId());
                        showListView();
                        return true;
                }
                return super.onMenuItemSelected(featureId, item);
            }

            private void createDiary(){

                Intent intent = new Intent();
                intent.setClass(this, ActivityDiary.class);
                startActivityForResult(intent, ACTIVITY_CREATE);
            }
        }
    }
}
package cn.dccssq;

        import android.app.Activity;
        import android.content.Intent;
        import android.os.Bundle;
        import android.util.Log;
        import android.view.View;
        import android.widget.Button;
        import android.widget.EditText;

public class ActivityDiary extends Activity {

    // title EditText
    EditText titleTxt;
    // body EditText
    EditText bodyTxt;
    // save Button
    Button btn;
    // Row Id
    Long rowId;
    private DiaryDbAdapter diaryDb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.notepad);

        // Initialize the DiaryDbAdapter.
        diaryDb = new DiaryDbAdapter(this);

        // Get the screen control
        titleTxt = (EditText)findViewById(R.id.title);
        bodyTxt = (EditText)findViewById(R.id.body_text);
        btn = (Button)findViewById(R.id.button);
        rowId = null ;

        // Get data from the front page
        Bundle bundle = getIntent().getExtras();

        if(bundle!=null){
            Log.i("bund:",bundle.toString());

            // Set data to page
            String title = bundle.getString(DiaryDbAdapter.KEY_TITLE);
            String body = bundle.getString(DiaryDbAdapter.KEY_BODY);
            rowId = bundle.getLong(DiaryDbAdapter.KEY_ROWID);
            if(title!=null)
            {
                titleTxt.setText(title);
            }
            if(body!=null)
            {
                bodyTxt.setText(body);
            }
        }

        btn.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                String title = titleTxt.getText().toString();
                String body = bodyTxt.getText().toString();
                if(checkInput(title, body)){
                    diaryDb.open();
                    if(rowId!=null){
                        diaryDb.updateDiary(rowId, title, body);
                    }else{
                        diaryDb.createDiary(title, body);
                    }
                    diaryDb.close();

                    Intent mIntent = new Intent();
                    setResult(RESULT_OK, mIntent);

                    finish();
                }
            }
        });
    }

    /**
     * Validate the input.
     * @param title
     * @param body
     * @return
     */
    public boolean checkInput(String title ,String body){

        if(null==title || title.trim().length()==0){
            titleTxt.setError("Please input the title!");
            return false;
        }

        if(null==body || body.trim().length()==0){
            bodyTxt.setError("Please input the content!");
            return false;
        }
        return true;
    }
}

package cn.dccssq;

        import android.app.ListActivity;
        import android.content.Intent;
        import android.database.Cursor;
        import android.os.Bundle;
        import android.util.Log;
        import android.view.Menu;
        import android.view.MenuItem;
        import android.view.View;
        import android.widget.ListAdapter;
        import android.widget.ListView;
        import android.widget.SimpleCursorAdapter;

public class MainActivity extends ListActivity {

    private static final int INSERT_ID = Menu.FIRST;

    private static final int DELETE_ID = Menu.FIRST + 1;

    private static final int ACTIVITY_CREATE = 0;

    private static final int ACTIVITY_EDIT = 1;

    private DiaryDbAdapter diaryDb;

    private Cursor cursor;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        diaryDb =new DiaryDbAdapter(this);
        diaryDb.open();

    }

    private void showListView(){
        cursor = diaryDb.getAllNotes();

        String[] from = new String[]{DiaryDbAdapter.KEY_TITLE,DiaryDbAdapter.KEY_BODY};
        int[] to = new int[]{R.id.text1,R.id.created};

        ListAdapter cursorAdapter = new SimpleCursorAdapter(this,R.layout.diary_row,cursor,from,to);
        setListAdapter(cursorAdapter);
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        super.onListItemClick(l, v, position, id);
        Cursor c = cursor;
        c.move(position);
        Intent intent = new Intent(this,ActivityDiary.class);
        intent.putExtra(DiaryDbAdapter.KEY_ROWID, id);
        intent.putExtra(DiaryDbAdapter.KEY_TITLE, c.getString(c
                .getColumnIndexOrThrow(DiaryDbAdapter.KEY_TITLE)));
        intent.putExtra(DiaryDbAdapter.KEY_BODY, c.getString(c
                .getColumnIndexOrThrow(DiaryDbAdapter.KEY_BODY)));
        startActivityForResult(intent, ACTIVITY_EDIT);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        showListView();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // TODO Auto-generated method stub
        super.onCreateOptionsMenu(menu);
        menu.add(0,INSERT_ID,0,R.string.menu_insert);
        menu.add(0,DELETE_ID,0,R.string.menu_delete);
        return true;
    }

    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        // TODO Auto-generated method stub
        switch(item.getItemId()){
            case INSERT_ID:
                Log.i("INSERT:", String.valueOf(INSERT_ID));
                createDiary();
                return true;
            case DELETE_ID:
                Log.i("DELETE_ID:", String.valueOf(getListView().getSelectedItemId()));
                diaryDb.deleteDiary(getListView().getSelectedItemId());
                showListView();
                return true;
        }
        return super.onMenuItemSelected(featureId, item);
    }

    private void createDiary(){

        Intent intent = new Intent();
        intent.setClass(this, ActivityDiary.class);
        startActivityForResult(intent, ACTIVITY_CREATE);
    }
}
package cn.dccssq;

        import android.content.Context;
        import android.database.sqlite.SQLiteDatabase;
        import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper {

    private final static String DATABSE_NAME = "notepad";
    private final static int DATABASE_VERSION = 1;
    public final static String DATABSE_TABLE = "diary";

    private final static String DATABASE_CREATE = "create table " + DATABSE_TABLE + " (_id integer primary key autoincrement,"
            + "title text not null, body text not null, created text not null);";
    public DatabaseHelper(Context context) {
        super(context, DATABSE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(DATABASE_CREATE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
        db.execSQL("DROP TABLE IF EXISTS " + DATABSE_TABLE);
        onCreate(db);
    }

}
